<template>
  <div>
    <el-form-item label="入场动画"></el-form-item>
    <div class="animation-box">
      <div class="animation-item" v-for="(item, index) in animateList" :key="index">
        <div class="animation-item-img" :class="{ active: item.value === editorForm.aos.value }"
          @click="handleSelectAnimate(item.value)">
          <!-- <i class="iconfont animation-icon">&#xe712;</i> -->
          <div class="animation-icon" data-aos-once="false" :data-aos="item.value"
            @mouseenter="iconMouseenter($event, item)" @mouseleave="iconMouseleave($event, item)" :style="{
              'background-position': item.backgroundPosition,
              transform: item.rotate
            }"></div>
          <div v-show="item.value === editorForm.aos.value" class="check-box">
            <el-icon :size="12" color="#fff">
              <Check />
            </el-icon>
          </div>
        </div>
        <span class="animation-item-name">{{ item.name }}</span>
      </div>
      <!-- 最后一排样式调整 -->
      <div v-for="n in 3" :key="n" style="width: calc((100% - 40px) / 4); height: 0"></div>
    </div>
  </div>
</template>

<script setup>
import { inject, reactive } from 'vue'
import { Check } from '@element-plus/icons-vue'

const { editorForm, backUpdate } = inject('commonForm')
const animateList = reactive([
  { name: '无效果', value: '', backgroundPosition: '-1982px 0' },
  { name: '向上淡入', value: 'fade-up', backgroundPosition: '-1982px 0', rotate: 'rotate(90deg)' },
  { name: '向下淡入', value: 'fade-down', backgroundPosition: '-1982px 0', rotate: 'rotate(-90deg)' },
  { name: '向左淡入', value: 'fade-left', backgroundPosition: '-1982px 0', rotate: 'rotate(0deg)' },
  { name: '向右淡入', value: 'fade-right', backgroundPosition: '-1982px 0', rotate: 'rotate(180deg)' },
  { name: '缩放进入', value: 'zoom-in', backgroundPosition: '-2070px -255px' },
  { name: '缩放退出', value: 'zoom-out', backgroundPosition: '-1982px -340px' },
  { name: '向上滑动', value: 'slide-up', backgroundPosition: '-2070px -85px', rotate: 'rotate(90deg)' },
  { name: '向下滑动', value: 'slide-down', backgroundPosition: '-2070px -85px', rotate: 'rotate(-90deg)' },
  { name: '向左滑动', value: 'slide-left', backgroundPosition: '-2070px -85px', rotate: 'rotate(0deg)' },
  { name: '向右滑动', value: 'slide-right', backgroundPosition: '-2070px -85px', rotate: 'rotate(180deg)' },
  { name: '向上翻转', value: 'flip-up', backgroundPosition: '-1982px -255px', rotate: 'rotate(90deg)' },
  { name: '向下翻转', value: 'flip-down', backgroundPosition: '-1982px -255px', rotate: 'rotate(-90deg)' },
  { name: '向左翻转', value: 'flip-left', backgroundPosition: '-1982px -255px', rotate: 'rotate(0deg)' },
  { name: '向右翻转', value: 'flip-right', backgroundPosition: '-1982px -255px', rotate: 'rotate(180deg)' }
])
const handleSelectAnimate = value => {
  editorForm.value.aos.value = value
  backUpdate()
}
const iconMouseenter = (event, item) => {
  if (!item.value) return
  const element = event.target
  // 添加动画类
  element.setAttribute('data-aos-animate', '')
  element.classList.add('aos-animate')
}
const iconMouseleave = (event, item) => {
  if (!item.value) return
  const element = event.target
  // 只移除动画类，保持元素可见
  element.classList.remove('aos-animate')
  element.removeAttribute('data-aos-animate')
}

</script>

<style lang='scss' scoped>
.animation-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  margin-top: -15px;

  .animation-item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-top: 10px;
    margin-bottom: 6px;
    width: calc((100% - 40px) / 4);
    cursor: pointer;

    .animation-item-img {
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 70px;
      height: 70px;
      border: 1px solid #f2f2f2;
      border-radius: 4px;

      .animation-icon {
        width: 100%;
        height: 100%;
        background: url(~@/assets/mbg02.png);
        opacity: 1; // 确保默认可见
        visibility: visible; // 确保默认可见
      }

      .check-box {
        position: absolute;
        top: 0;
        right: 0;
        line-height: 0;
        width: 16px;
        height: 16px;
        border-end-start-radius: 100%;
        text-align: center;
        background: $mainColor;
      }
    }

    .animation-item-img.active {
      border-color: $mainColor;
    }

    .animation-item-name {
      margin-top: 4px;
      line-height: 14px;
      font-size: 12px;
      color: #606266;
    }
  }
}
</style>